/*
 *  Connection Factory
 */
package AcessoBD;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ConnectionFactory {
    
    private Connection conn;
    
    public ConnectionFactory(){
        this.conn = null;
    }

    public Connection getAdminConnection() throws DAOException {
        this.conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            conn = DriverManager.getConnection("jdbc:mysql:" +
                    "//localhost/nucleo_agroecologia_apete_caapua?user=adminApete&password=apeteNucleoAdmin241");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    public Connection getMemberConnection() throws DAOException {
        this.conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            conn = DriverManager.getConnection("jdbc:mysql:" +
                    "//localhost/nucleo_agroecologia_apete_caapua?user=membroApete&password=membro_nucleo_ufscar391");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    public Connection getNormalConnection() throws DAOException {
        this.conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            conn = DriverManager.getConnection("jdbc:mysql:" +
                    "//localhost/nucleo_agroecologia_apete_caapua?user=visitanteApete&password=visitanteNucleoApete102");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    public void closeConnection(Statement stmt, ResultSet rs)
            throws DAOException {
        close(this.conn, stmt, rs);
    }

    public void closeConnection(Statement stmt)
            throws DAOException {
        close(this.conn, stmt, null);
    }

    public void closeConnection()
            throws DAOException {
        close(this.conn, null, null);
    }

    private void close(Connection conn, Statement stmt, ResultSet rs)
            throws DAOException {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
